/**
 * @file:          MT6816_14bit.h
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.06.09
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.06.09,18:31:37
 */
#ifndef _MT6816_14BIT_H_
#define _MT6816_14BIT_H_

/* Include Files **************************************************************/
#include "Datatype.h"
// #include "STC32G8K64.h"
#include "ch32v30x_module.h"
#include "encoder_cal_table.h"
#include "EncoderCalibrator.h"
#include "MT68xx.h"

/* Macro Definition ***********************************************************/
#define MT6816_PACKAGE_BYTES 4

/* Data Type Define ***********************************************************/
enum MT6816_Reg
{
    MT6816_REG_ANGLE_DATA_REG_H = 0x03,
    MT6816_REG_ANGLE_DATA_REG_L = 0x04,
};

enum MT6816_Cmd
{
    MT6816_CMD_WRITE_REG = 0x00,
    MT6816_CMD_READ_REG = 0x80,
};

union MT6816_EncoderDataFrame
{
    uint32 all;
    uint8 u8data[4];
    struct
    {
        uint32 parity : 1;
        uint32 mag_warning : 1;
        uint32 angle_pulse : 14;
        uint32 reserve : 16;
    } bits;
};

/* Variable Declare ***********************************************************/

/* Inline Function Define *****************************************************/

/* Function Declare ***********************************************************/
uint8 MT6816_14bit_Init(struct MT68xx *self);
int32 MT6816_14bit_SingleToMultiTurns(struct MT68xx *self);
int32 MT6816_14bit_GetEncoder(struct MT68xx *self);
int32 MT6816_14bit_EncoderRawRead(struct MT68xx *self);
int32 MT6816_14bit_GetEncoderCalibrated(struct MT68xx *self);
int32 MT6816_14bit_GetEncoderSingleturn(struct MT68xx *self);
uint8_t MT6816_14bit_GetEncoderCalibrationStatus(struct MT68xx *self);
uint16_t MT6816_14bit_WriteReg(struct MT68xx *self, uint16_t reg_addr, uint8_t *data_buff, uint8_t length);
uint16_t MT6816_14bit_ReadReg(struct MT68xx *self, uint16_t reg_addr, uint8_t *data_buff, uint8_t length);
uint16_t MT6816_14bit_SaveIntoEEPROM(struct MT68xx *self);

#endif